<?php
namespace  app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
//商品页
class Goods extends BaseController {
    //goods商品列表
     public function goodList(){
         //where条件拼接
        $where=GoodsLogic::goodWhere();
        //留值
         $data=GoodsLogic::goodData();
         View::assign($data);
         View::assign('data',$data);
         //order排序
         $order=GoodsLogic::goodOrder();
         View::assign($order);
         //分类
         $cate=GoodsLogic::goodCate();
         View::assign('cate',$cate);
         //品牌
         $pin=\app\admin\model\Goods::goodsPin();
         View::assign('pin',$pin);
        //语句
       $goods=\app\admin\model\Goods::goodsList($where,$order,$data);
         //关键字搜索变色
         $goods=GoodsLogic::goodWord($goods);
         $page=$goods->render();
         View::assign(['goods'=>$goods,'page'=>$page]);
          return View::fetch();
     }

     //goods  商品添加
     public function goodAdd(){
         if (Request::isPost()){
             $post=Request::post();
             list($status, $paths) = Upload::imgUpload('default_image');
             if ($status && !empty($paths)) {
                 $post['default_image'] = $paths;
             }
             list($status, $pathss) = Upload::imgUpload('images');
             if ($status && !empty($pathss)) {
                 $post['images'] = $pathss;
             }
             $post['add_time']=time();
            $res=\app\admin\model\Goods::getGoodsAdd($post);
            if ($res){
                $this->success('添加成功',url('goodList'));
            }else{
                $this->error('添加失败');
            }
         }else{
             //分类
             $cate=GoodsLogic::goodCate();
             View::assign('cate',$cate);
             //品牌
             $pin=\app\admin\model\Goods::goodsPin();
             View::assign('pin',$pin);
             return View::fetch();
         }
     }

     //goods  商品修改
     public function goodEdit(){
         if (Request::isPost()){
             $param=Request::param();
             //单图上传临时目录文件转到正式目录文件
             list($status,$default_image) =Upload::imgUpload('default_image');
             if (!$status){
                 $this->error($default_image);
             }
             if ($status && !empty($default_image)){
                 list($path_date, $path_dir)=GoodsLogic::setImgUrl($default_image,'goods/');
                 $param['default_image']=$path_dir;
             }
         //多图临时目录文件转到正式目录文件
         list($old_image,$new_image)=GoodsLogic::getImgsUrl('goods/');
             $param['images']=implode(',',$old_image);
             $obj=\app\admin\model\Goods::find( $param['goods_id']);
             //老的单图图片路径
             $old=$obj->default_image;
             //多图的老图路径
             $old_img=$obj->images;
            $res=\app\admin\model\Goods::getGoodsEdit($param);
             if ($res){
                 //移动临时目录到正式目录
                 GoodsLogic::getImgDir($new_image,'goods/','./images/');
             //删除多图中被替换的图片路径
             if (isset($param['old_imgs']) && !empty($old_img)){
                 //array_diff 参数1中的数据 参数2不存在会被取出  作差值
                 $arr=array_diff($old_img,$param['old_imgs']);
             }else{
                 $arr=$old_img;

             }
             if (is_array($arr) && !empty($arr)){
                 //删除老图路径
                 foreach ($arr as $v){
                     if (!empty($v)){unlink('./images/'.$v);}
                 }
             }
            //单图移动临时目录到正式目录
             if ($status && !empty($default_image)) {
                 GoodsLogic::setImgDir($default_image, $path_date, $path_dir, './images/');
             }
                //删除单图老图路径
             if ($status && !empty($default_image) &&!empty($old)){unlink('./images/'.$old);}
                 $this->success('修改成功',url('goodList'));
         }else{
             $this->error('修改失败');
         }
         }else{
             //分类
             $cate=GoodsLogic::goodCate();
             View::assign('cateList',$cate);
             //品牌
             $pin=\app\admin\model\Goods::goodsPin();
             View::assign('brandList',$pin);
             $param=Request::param();
             $find=\app\admin\model\Goods::getGoodsEdit($param);
             View::assign($find->toArray());
             return View::fetch();
         }

      }


    //goods  商品删除
     public function goodDel(){
         $get=Request::get();
         $res=\app\admin\model\Goods::getGoodsDel($get);
                 if ($res){
             $this->success('删除成功',url('goodList'));
         }else{
             $this->error('删除失败');
         }


      }


      //goods 商品软删除
      public function goodAllDel(){
         $get=Request::get();
         $res=\app\admin\model\Goods::getGoodsAllDel($get);
          if ($res){
              $this->success('删除成功',url('goodList'));
          }else{
              $this->error('删除失败');
          }
      }

        //goods 商品回收站
     public function goodsRecycle(){
         $list=\app\admin\model\Goods::GoodsRecycle();
         View::assign('list',$list);

          return View::fetch();
      }

      //goods 商品回收站恢复
     public function goodHuiFu(){
         $get=Request::get();
         $res=\app\admin\model\Goods::recyclehuifu($get);
         if ($res){
             $this->success('恢复成功',url('goodList'));
         }else{
             $this->error('恢复失败');
         }

         return View::fetch();
     }

     //goods  商品回收站删除
      public function goodHuiDel(){
          $get=Request::get();
          $res=\app\admin\model\Goods::recyclehuiDel($get);
          if ($res){
              $this->success('删除成功',url('goodsRecycle'));
          }else{
              $this->error('删除失败');
          }
          return View::fetch();
      }



}